package com.study.springmvc.service.user.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;

import com.study.springmvc.biz.module.common.FailureEnum;
import com.study.springmvc.biz.module.common.Result;
import com.study.springmvc.biz.module.user.UserVO;
import com.study.springmvc.dal.daointerface.user.UserDAO;
import com.study.springmvc.dal.dataobject.user.UserDO;
import com.study.springmvc.service.impl.BaseServiceImpl;
import com.study.springmvc.service.user.UserService;

public class UserServiceImpl extends BaseServiceImpl implements UserService {

    @Autowired
    public UserDAO userDAO;

    public Result checkUserLogin(UserVO userVO) {
        Result result = new Result();
        DetachedCriteria query = DetachedCriteria.forClass(UserDO.class);
        query.add(Restrictions.eq("userName", userVO.getUserName())).add(
            Restrictions.eq("password", userVO.getPassword()));
        UserDO UsersDO = (UserDO) userDAO.findObjectByCriteria(query);
        if (UsersDO != null) {
            result.setSuccess(true);
            result.setException(null);
            result.setFailure(null);
        } else {
            result.setSuccess(false);
            result.setFailure(FailureEnum.LOGIN_FAILURE);
            result.setFailureDetail(FailureEnum.LOGIN_FAILURE.getDetail());
        }
        return result;
    }

    public Result checkUserName(String userName) {
        Result result = new Result();
        DetachedCriteria query = DetachedCriteria.forClass(UserDO.class);
        query.add(Restrictions.eq("userName", userName));
        Collection<UserDO> list = userDAO.findAll(query);
        if (list.size() == 0) {
            result.setSuccess(true);
            result.setException(null);
            result.setFailure(null);
        } else {
            result.setSuccess(false);
            result.setFailure(FailureEnum.USER_LOGIN_NAME_EXIS);
            result.setFailureDetail(FailureEnum.USER_LOGIN_NAME_EXIS.getDetail());
        }
        return result;
    }

    public Result registerUser(UserVO userVO) {
        // TODO Auto-generated method stub
        return null;
    }

    public Result getUserByName(String userName) {
        // TODO Auto-generated method stub
        Result result = new Result();
        DetachedCriteria query = DetachedCriteria.forClass(UserDO.class);
        query.add(Restrictions.eq("userName", userName));
        Collection<UserDO> list = userDAO.findAll(query);
        List<UserDO> listL = (ArrayList<UserDO>) list;
        //        if (list != null && list.size() > 0) {
        //            result.setSuccess(true);
        //            result.set
        //            return (Result) list.iterator().next();
        //        }
        return null;
    }
}
